'==============================================================================
' LANG		: VBScript 
' NAME		: FCS-SampleScript XPSP2 Hotfix Check Install.vbs 
' VERSION	: 1.0007 3/2/2007
' Description 	: Script to install the prerequisite FCS XPSP2 Hotfix KB914882  
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
' Copyright (C) 2007. Microsoft Corporation.  All rights reserved.
'
' NOTES		: ALL SAMPLE SCRIPTS BEGIN WITH THE SAME SETUP ELEMENTS
'                 You can use this script standalone or incoporate the 
'                 subroutines in this script into a larger script.
'==============================================================================
Option Explicit
Dim bDEBUG : bDEBUG=True 'Set to TRUE to help with script debugging.
' Constants Declarations
  Const WAIT_ON_RETURN = True
  Const DO_NOT_WAIT_ON_RETURN = False
  Const HIDE_WINDOW = 0
  Const SHOW_WINDOW = 1
  Const ThisComputer = "."
  '==========================================================
' Main Routine
  On Error Resume Next
  If bDEBUG then On Error Goto 0
  
' Create system objects
  Dim WshShell      : Set WshShell = WScript.CreateObject("WScript.Shell")
  Dim objRegistry   : Set objRegistry = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
			                            ThisComputer & "\root\default:StdRegProv")
  Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
                                                     ThisComputer & "\root\cimv2")

' Call the installation functions
  Call FCS_InstallXPSP2Hotfix
  
' Clean up objects and exit
  Set objWMIService = Nothing
  Set objRegistry = Nothing
  Set WshShell = Nothing
  
  wscript.quit
  
'================================================================================
' SubRoutine FCS_InstallXPSP2Hotfix
'      Checks for prerequisite XPSP2 Hotfix (KB914882) and
'      installs it if it is missing
'  INPUTS: None
'  OUTPUT: None
' ASSUMES: The existence of the WshShell, objRegistry, and objWMIService objects
'          The Hotfix executable is in the folder the script was executed from
'          If this isn't true you will need to provide the full path name to the 
'          to the Hotfix installation file
'================================================================================
  Private Sub FCS_InstallXPSP2Hotfix()
  
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const OSVersionRegKey = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion" 
    Dim   OSVersion : OSVersion = WshShell.RegRead(OSVersionRegKey)
    If bDEBUG then wscript.echo "DEBUG: Current OS version is " & OSVersion
  
    Const XPSP2 = "5.1"
    If OSVersion = XPSP2 Then
	
        Dim SubKey, SubKeyArray()
	Dim QFEIsInstalled : QFEIsInstalled = False
	
	Const HotFixRegKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\HotFix"
	objRegistry.EnumKey HKEY_LOCAL_MACHINE, HotFixRegKey, SubKeyArray
		
    If bDEBUG then wscript.echo "DEBUG: Searching for previous QFE installation." & OSVersion

	Const ReqdQFE = "KB914882"
	For Each SubKey In SubKeyArray

'	    If bDEBUG then wscript.echo "DEBUG: Current key is " & strSubKey

	    If StrComp(ReqdQFE, Trim(UCase(SubKey)), vbTextCompare)=0 Then 
		QFEIsInstalled = True
		Exit For
	    End If
	Next
	If NOT QFEIsInstalled Then
           
    If bDEBUG then wscript.echo "DEBUG: XPSP2 Hotfix not installed, running windowsxp-kb914882-x86-enu.exe"
   	    WshShell.Run "windowsxp-kb914882-x86-enu.exe /quiet /norestart",HIDE_WINDOW,WAIT_ON_RETURN
        Else

    If bDEBUG then wscript.echo "DEBUG: XPSP2 Hotfix already installed." & OSVersion

	End If
    End If

  End Sub